package com.leectrl.utils;

import com.leectrl.bean.HttpLabel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class HttpRouter extends ClassLoader {

    private Map<String, Action> httpRouterAction = new HashMap<>();

  //  private String classpath = this.getClass().getClassLoader().getResource("/").getPath();
  //  private String classpath="";
    private Map<String, Object> controllerBeans = new HashMap<String, Object>();
    private static HttpRouter httpRouter=null;

    public static HttpRouter getInstance(){
        if(httpRouter==null){
            httpRouter=new HttpRouter();
        }
        return httpRouter;
    }

    private HttpRouter(){

    }

//    @Override
//    protected Class<?> findClass(String name) throws ClassNotFoundException {
//        String path = classpath + name.replaceAll("\\.", "/");
//        byte[] bytes;
//        try (InputStream ins = new FileInputStream(path)) {
//            try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
//                byte[] buffer = new byte[1024 * 5];
//                int b = 0;
//                while ((b = ins.read(buffer)) != -1) {
//                    out.write(buffer, 0, b);
//                }
//                bytes = out.toByteArray();
//            }
//        } catch (Exception e) {
//            throw new ClassNotFoundException();
//        }
//        return defineClass(name, bytes, 0, bytes.length);
//    }

    public void addRouter(Class cls) {
        try {
            Method[] methods = cls.getDeclaredMethods();
            for (Method invokeMethod : methods) {
                Annotation[] annotations = invokeMethod.getAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation.annotationType() == RequestMapping.class) {
                        RequestMapping requestMapping = (RequestMapping) annotation;
                        String uri = requestMapping.uri();
                        boolean auth = requestMapping.auth();
                        String httpMethod = requestMapping.method().toUpperCase();
                        // 保存Bean单例
                        if (!controllerBeans.containsKey(cls.getName())) {
                            controllerBeans.put(cls.getName(), cls.newInstance());
                        }
                        Action action = new Action(controllerBeans.get(cls.getName()), invokeMethod);
                        action.setCheckSession(requestMapping.auth());
                        // 保存映射关系
                        httpRouterAction.put(uri+"&"+httpMethod.toLowerCase(), action);
                        System.out.println(uri+"&"+httpMethod.toLowerCase());
                    }
                }
            }
        } catch (Exception e) {

        }
    }

    public Action getRoute(HttpLabel httpLabel) {
        System.out.println(httpLabel.getUri()+"&"+httpLabel.getHttpMethod().toString().toLowerCase());
        return httpRouterAction.get(httpLabel.getUri()+"&"+httpLabel.getHttpMethod().toString().toLowerCase());
    }
}
